package com.report.servlet;

import com.report.bean.GWBean;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReportServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println(this.getClass().getResource("/"));

        List<JasperPrint> reports = new ArrayList<JasperPrint>(2);

        String destFileName = "C:\\Users\\bao\\Desktop\\";
        String bbnd = req.getParameter("bbnd");
        if(null != bbnd && !"".equals(bbnd)){
            destFileName += (bbnd+".jasper");
        }else{
            destFileName += "gwReport.jasper";
        }


        File destFile = new File(destFileName);

        // 参数值
        Map parameters = new HashMap();

        // 封装的数据结果集
        List<GWBean> provider = new ArrayList<>();

        GWBean gwBean = new GWBean();
        gwBean.setGwTitle("收到了防控就是的弗兰克上岛咖啡发鬼地方个的发鬼地方个电饭锅第三方和电收到了防控就是的弗兰克上岛咖啡发鬼");
        gwBean.setHqMind("莱克斯顿弗兰克离开复工后立刻 ；给对方东方胜多负少wekrkw'iwieer看附件；山东科技V型参考链接在不可少的反馈水电费颗粒剂" +
//                "李开复撒的发生的水电费刷卡机电饭锅电饭锅电饭锅森岛帆高 对方告诉对方 是打发斯蒂芬撒对方水电费按时大事对方水电费撒旦水电费是撒旦" +
                "是东方水电费撒旦发生的森岛帆高水电费森岛帆高发给对方电饭锅多岁的股份撒旦方式大是大非是东方水电费撒旦法是对方水电费啥都说电" +
                "风扇");
        provider.add(gwBean);

        FileInputStream is = new FileInputStream(destFile);

        try {

//            JasperPrint print = JasperFillManager.fillReport(is, parameters, new JRBeanCollectionDataSource(provider));
//
//            reports.add(print);
//
//            ServletOutputStream ouputStream = resp.getOutputStream();
//            ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
//            oos.writeObject(reports);


            byte[] bytes =
                    JasperRunManager.runReportToPdf(
                            destFile.getPath(),
                            parameters,
                            new JRBeanCollectionDataSource(provider)
                    );

            resp.setContentType("application/pdf");
            resp.setContentLength(bytes.length);
            ServletOutputStream ouputStream = resp.getOutputStream();
            ouputStream.write(bytes, 0, bytes.length);
            ouputStream.flush();
            ouputStream.close();

        } catch (JRException e) {
            e.printStackTrace();
        }

    }
}
